﻿using System;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

public class Missile : MonoBehaviour
{
    private int curPiercingCount;
    public RangeWeapon emitter;
    public float gravity;
    private Collider lastHitCollider;
    public LayerMask layerMask;
    private float lifetime;
    public Action onDie;
    public Action<RaycastHit> onHit;
    public int piercingCount;
    public float radius = 0.2f;
    public float startLifetime = 5f;
    public Character user;

    public void Die()
    {
        if (this.onDie != null)
        {
            this.onDie();
        }
        this.Recycle<Missile>();
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.DrawWireSphere(base.transform.position, this.radius);
    }

    private void Raycast(Vector3 start, Vector3 dir)
    {
        <Raycast>c__AnonStorey53 storey = new <Raycast>c__AnonStorey53 {
            <>f__this = this
        };
        if (Physics.SphereCast(start, this.radius, dir, out storey.hit, dir.magnitude, (int) this.layerMask) && (storey.hit.collider != this.lastHitCollider))
        {
            Entity entity = storey.hit.collider.GetEntity();
            if ((entity == null) || !entity.GetComponentsInChildren(typeof(IMissileDefender)).Cast<IMissileDefender>().Any<IMissileDefender>(new Func<IMissileDefender, bool>(storey.<>m__88)))
            {
                if (this.onHit != null)
                {
                    this.onHit(storey.hit);
                }
                this.curPiercingCount++;
                if (this.curPiercingCount > this.piercingCount)
                {
                    this.lifetime = 0f;
                }
                this.lastHitCollider = storey.hit.collider;
            }
        }
    }

    private void Start()
    {
        this.lifetime = this.startLifetime;
        this.curPiercingCount = 0;
        this.lastHitCollider = null;
        if (this.user != null)
        {
            Vector3 start = this.user.transform.position.SetY(base.transform.position.y);
            this.Raycast(start, base.transform.position - start);
        }
    }

    private void Update()
    {
        this.velocity += (Vector3) ((Physics.gravity * this.gravity) * Time.deltaTime);
        Vector3 dir = (Vector3) (this.velocity * Time.deltaTime);
        this.Raycast(base.transform.position, dir);
        Transform transform = base.transform;
        transform.position += dir;
        if (this.velocity != Vector3.zero)
        {
            base.transform.rotation = Quaternion.LookRotation(this.velocity);
        }
        this.lifetime -= Time.deltaTime;
        if (this.lifetime <= 0f)
        {
            this.Die();
        }
    }

    public Vector3 velocity { get; set; }

    [CompilerGenerated]
    private sealed class <Raycast>c__AnonStorey53
    {
        internal Missile <>f__this;
        internal RaycastHit hit;

        internal bool <>m__88(IMissileDefender x)
        {
            return x.Defence(this.<>f__this, this.hit);
        }
    }
}

